package org.ypx.shared.http.base;

import org.ypx.shared.util.GsonUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


public abstract class ObjectCallback<T, RESP extends ObjectResp<T>> {

	private Class<RESP> responseClass = null;
    private Object para = null;
    public Type respType = null;

    @SuppressWarnings("unchecked")
    protected ObjectCallback() {
        try {
            Type superType = this.getClass().getGenericSuperclass();
            if (superType instanceof ParameterizedType) {
                Type[] types = ((ParameterizedType) superType).getActualTypeArguments();
                if (types != null && types.length == 2) {
                    this.responseClass = (Class<RESP>) types[1];
//                    respType = types[1].getClass().getGenericSuperclass();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected abstract T onHandleResult(RESP resp, Object obj);

    /**
     * @param code 错误码
     * @param obj
     */
    protected abstract void onSafeErr(int code, String msg, Object obj);
    
    protected abstract void onSafeSucc(T resultData, Object obj);
    
    /**
     * 设置调用上下文，会在callback时一起原样返回
     * 
     * @param para
     */
    public void setPara(Object para) {
        this.para = para;
    }

    final public T onFinished(String result) {
        try {
            return handleResult(result, para);
        } catch (final Exception e) {
        	onSafeErr(102, e.toString(), para);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    final private T handleResult(String response, final Object obj) {
        if (response != null) {

            final BaseDo data = (BaseDo) GsonUtil.fromJson(response, responseClass);
//            if (data.isApiSuccess()) {
                // 请求成功
                T result = null;
                try {
                    result = onHandleResult((RESP) data, obj);
                } catch (final Exception e) {
                    e.printStackTrace();
                    onSafeErr(103, e.toString(), obj);
                }

//                if (result != null) {
//                    // 数据整理成功，回调
//                    final T finalResult = result;
//                    try {
//                        onSafeSucc(finalResult, obj);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        onSafeErr(104, e.toString(), obj);
//                    }
//                }else {
//                    // error
//                	onSafeErr(105, "null result", obj);
//                }
                return result;
            } else {
//            	String errorMsg = data.getMsg();
//                Object returnParam = obj == null ? data : obj;
//                onSafeErr(106, errorMsg, returnParam);
            }
//        } else {
//            onSafeErr(107, "", obj);
//        }
        return null;
    }
}
